package com.nintendo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nintendo.exceptions.GraceException;
import com.nintendo.mapper.FriendRequestMapper;
import com.nintendo.mapper.FriendShipMapper;
import com.nintendo.pojo.FriendInfoVo;
import com.nintendo.pojo.FriendRequest;
import com.nintendo.pojo.Friendship;
import com.nintendo.pojo.bo.NewFriendRequestBO;
import com.nintendo.result.ResponseStatusEnum;
import com.nintendo.service.FriendShipService;
import com.nintendo.utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class FriendShipServiceImpl extends ServiceImpl<FriendShipMapper, Friendship> implements FriendShipService {
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private FriendRequestMapper friendRequestMapper;
    @Autowired
    private FriendShipMapper friendShipMapper;
    @Override
    public void sendRequest(NewFriendRequestBO newFriendRequestBO, HttpServletRequest request) {
        LambdaQueryWrapper<FriendRequest> friendRequestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        friendRequestLambdaQueryWrapper.eq(FriendRequest::getMyId,newFriendRequestBO.getMyId())
                .eq(FriendRequest::getFriendId,newFriendRequestBO.getFriendId());
        FriendRequest deleteFriendRequest = friendRequestMapper.selectOne(friendRequestLambdaQueryWrapper);
        if (deleteFriendRequest != null) {
            friendRequestMapper.delete(friendRequestLambdaQueryWrapper);
        }
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setId(String.valueOf(snowFlake.nextId()));
        BeanUtils.copyProperties(newFriendRequestBO,friendRequest);
        friendRequest.setVerifyStatus(0);
        friendRequest.setRequestTime(LocalDateTime.now());
        friendRequestMapper.insert(friendRequest);
    }

    @Override
    public void rejected(String friendRequestId) {
        friendRequestMapper.rejectedRequest(friendRequestId);
    }
    @Transactional
    @Override
    public void accept(String friendRequestId) {
        FriendRequest friendRequest = friendRequestMapper.selectById(friendRequestId);
        if(friendRequest ==null){
            GraceException.display(ResponseStatusEnum.GOOD_FRIEND_REQUEST_NOT_EXIST);
        }
        // 更改请求信息为已同意
        friendRequestMapper.acceptRequest(friendRequestId);
        // 建立双方好友关系
        Friendship friendship = new Friendship();
        friendship.setId(String.valueOf(snowFlake.nextId()));
        friendship.setMyId(friendRequest.getMyId());
        friendship.setFriendId(friendRequest.getFriendId());
        friendship.setFriendRemark(friendRequest.getFriendRemark());
        friendship.setChatBg("https://i.loli.net/2020/09/07/5QZqZwXZQyZqZqZ.png");
        friendship.setIsMsgIgnore(0);
        friendship.setIsBlack(0);
        friendship.setCreatedTime(LocalDateTime.now());
        friendship.setUpdatedTime(LocalDateTime.now());
        Friendship friendship2 = new Friendship();
        friendship2.setId(String.valueOf(snowFlake.nextId()));
        friendship2.setMyId(friendRequest.getFriendId());
        friendship2.setFriendId(friendRequest.getMyId());
        friendship2.setFriendRemark("");
        friendship2.setChatBg("https://i.loli.net/2020/09/07/5QZqZwXZQyZqZqZ.png");
        friendship2.setIsMsgIgnore(0);
        friendship2.setIsBlack(0);
        friendship2.setCreatedTime(LocalDateTime.now());
        friendship2.setUpdatedTime(LocalDateTime.now());
        friendShipMapper.insert(friendship);
        friendShipMapper.insert(friendship2);
    }

    @Override
    public void toBeBlack(HttpServletRequest request, String friendId) {
        String userId = request.getHeader("userId");
        friendShipMapper.beBlack(userId,friendId);
    }

    @Override
    public void noBlack(HttpServletRequest request, String friendId) {
        String userId = request.getHeader("userId");
        friendShipMapper.noBlack(userId,friendId);
    }

    @Override
    public FriendInfoVo searchMyFriendUserInfo(HttpServletRequest request, String friendId) {
        String userId = request.getHeader("userId");
        System.out.println(userId+"======="+friendId);
        FriendInfoVo friendInfoVo =  friendShipMapper.findFriendInfo(userId,friendId);
        return friendInfoVo;
    }

    @Override
    public Friendship searchMyFriendUserInfoById(String friendShipId) {
        Friendship friendship =   friendShipMapper.findFriendUserInfoById((friendShipId));
        return friendship;
    }

    @Override
    public void updateFriendUserInfoById(String friendShipId, String remark) {
        Friendship friendUserInfoById = friendShipMapper.findFriendUserInfoById(friendShipId);
        friendUserInfoById.setFriendRemark(remark);
        friendShipMapper.updateById(friendUserInfoById);
    }


}
